import os


def set_langsmith():
    os.environ['LANGSMITH_ENDPOINT'] = "https://api.smith.langchain.com"
    os.environ['LANGCHAIN_TRACING_V2'] = "true"
    os.environ['LANGCHAIN_API_KEY'] = "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"
    os.environ["LANGCHAIN_PROJECT"] = "activity"


def set_env_vars():
    """
    Deprecated: 使用 load_dotenv() 代替
    """
    with open(os.path.abspath('.env'), 'r') as f:
        envs = f.readlines()
        for env in envs:
            if env.startswith('#'):
                continue
            vars = env.strip('\n').split('=')
            if len(vars) != 2:
                raise Exception('.env file not available')
            os.environ[vars[0].strip()] = vars[1].strip()


def get_env_by_key(key: str) -> str:
    value = os.getenv(key, False)
    if not value:
        raise Exception(f'cannot access [{key}] environment variable')
    return value


def get_env_port(key: str = 'SERVER_PORT'):
    if os.getenv(key, False) and int(os.getenv(key)):
        return int(os.getenv(key))
    else:
        raise Exception('cannot access [SERVER_PORT] environment variable')


def get_env_host(key: str = 'SERVER_HOST'):
    if os.getenv(key, False):
        return os.getenv(key)
    else:
        raise Exception('cannot access [SERVER_HOST] environment variable')
